/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public class Ship extends Sprite {
    
    protected Control control;
    protected GameManagement gameMng;
    protected int direction;
    
    protected long time = System.currentTimeMillis();
    protected int[] frameUp = {0, 16, 32, 48, 64, 80, 96, 112};
    protected int[] frameUpRight = {2, 18, 34, 50, 66, 82, 98, 114};
    protected int[] frameRight = {4, 20, 36, 52, 68, 84, 100, 116};
    protected int[] frameDownRight = {6, 22, 38, 54, 70, 86, 102, 118};
    protected int[] frameDown = {8, 24, 40, 56, 72, 88, 104, 120};
    protected int[] frameDownLeft = {10, 26, 42, 58, 74, 90, 106, 122};
    protected int[] frameLeft = {12, 28, 44, 60, 76, 92, 108, 124};
    protected int[] frameUpLeft = {14, 30, 46, 62, 78, 94, 110, 126};
    protected int[] frameTurn = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,15,16};
    public int[][] dirc = {{    -24,     -24,    0,    24,   24,   24,     0,    -24,    -24,     -24},
                          {      0,      -24,  -24,   -24,    0,    24,    24,    24,      0,    -24}};
    //                            7        8       1       2    3     4       5       6       7     8
    public Ship (GameManagement gameMng, Image image) throws Exception {
        
        super(image, 24, 24);
        
        this.gameMng = gameMng;   
        Random r = new Random();
        
        direction = Math.abs(r.nextInt() % 8) + 1;
        switch(direction) {
            
            case 1:setFrameSequence(frameUp);break;
            case 2:setFrameSequence(frameUpRight);break;
            case 3:setFrameSequence(frameRight);break;
            case 4:setFrameSequence(frameDownRight);break;
            case 5:setFrameSequence(frameDown);break;
            case 6:setFrameSequence(frameDownLeft);break;
            case 7:setFrameSequence(frameLeft);break;
            case 8:setFrameSequence(frameUpLeft);break;
        } 
    }
    
    public void moveUp () {
        
        direction = 1;
        setFrameSequence(frameUp);
    }
    
    public void moveUpRight () {
        
        direction = 2;
        setFrameSequence(frameUpRight);
    }
    
    public void moveRight () {
        
        direction = 3;
        setFrameSequence(frameRight);
    } 
    
    public void moveDownRight () {
        
        direction = 4;
        setFrameSequence(frameDownRight);
    }
    
    public void moveDown () {
        
        direction = 5;
        setFrameSequence(frameDown);
    }
    
    public void moveDownLeft () {
        
        direction = 6;
        setFrameSequence(frameDownLeft);
    } 
    
    public void moveLeft () {
        
        direction = 7;
        setFrameSequence(frameLeft);
    }
    
    public void moveUpLeft () {
        
        direction = 8;
        setFrameSequence(frameUpLeft);
    }  
    public void autoMove(Ship s,int oldx, int oldy)
    {//default
        oldx = s.getX();
        oldy = s.getY();
        if(getX() > oldx)
        {
            if(getY() > oldy)
                moveUpLeft();
            else if(getY() < oldy)
                    moveDownLeft();
            else moveLeft();
        }
        else
            if(getX() < oldx)
            {
                if(getY() > oldy)
                    moveUpRight();
                else if(getY() < oldy)
                        moveDownRight();
                else moveRight();
            }
        else    
            if(getY() > oldy)
                moveUp();
            else if(getY() < oldy)
                    moveDown();
    }
    public void fire () {
        
    }
    
    public void wobble(long currTime) {
            
        
        if(currTime - time > 150)
        {
            nextFrame();   
            time = currTime;
        }
    }
    public void trans(long currTime, int olddir)
    {
        setFrameSequence(frameTurn);
        setFrame(olddir * 2 - 1);
        while(getFrame() != (direction * 2 - 1))
            if(currTime - time > 50)
            {
                nextFrame();    
                time = currTime;
            }
    }
}
